package com.xnyzc.lhy.mis.service.impl.cms.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnyzc.lhy.common.component.convert.DateToStringConverter;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.EOrderType;
import com.xnyzc.lhy.common.penum.cms.EDriverComingUserType;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.comment.OaSysComment;
import com.xnyzc.lhy.mis.entity.order.OaOrderEvaluation;
import com.xnyzc.lhy.mis.entity.param.cms.PageVo;
import com.xnyzc.lhy.mis.entity.param.cms.user.CmsEvaluationParam;
import com.xnyzc.lhy.mis.entity.result.cms.user.CmsEvaluationListRes;
import com.xnyzc.lhy.mis.entity.user.OaCUser;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.mapper.comment.OaSysCommentMapper;
import com.xnyzc.lhy.mis.mapper.order.OaOrderEvaluationMapper;
import com.xnyzc.lhy.mis.mapper.user.OaCUserMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.service.cms.user.CmsEvaluationManageService;
import com.xnyzc.lhy.mis.service.cms.util.IOaSysGetOfficeIdListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 司机用户评价列表实现类
 *
 * @author zhaolijie
 * @date 2019-08-21 19:37
 */
@Service
public class CmsEvaluationManageServiceImpl implements CmsEvaluationManageService {

    @Autowired
    private IOaSysGetOfficeIdListService iOaSysGetOfficeIdListService;

    @Autowired
    private OaDUserMapper oadUserMapper;

    @Autowired
    private OaSysCommentMapper oaSysCommentMapper;

    @Autowired
    private OaCUserMapper oacUserMapper;

    @Autowired
    private OaOrderEvaluationMapper oaOrderEvaluationMapper;


    /**
     * 获取司机用户评价列表
     *
     * @param cmsEvaluationParam
     * @return
     */
    @Override
    public Rv getCmsEvaluationList(CmsEvaluationParam cmsEvaluationParam) {
        Integer page=cmsEvaluationParam.getPage();
        Integer pageSize=cmsEvaluationParam.getPageSize();
        String userName=cmsEvaluationParam.getUserName();
        String mobileAccount=cmsEvaluationParam.getMobileAccount();
        Integer queryType=cmsEvaluationParam.getQueryType();
        String orderNo=cmsEvaluationParam.getOrderNo();
        Long sysUserId=TokenUtil.getTokenUserId();
        List<String> dataScope=TokenUtil.getRoleDataScope();
        if(CheckUtil.objIsEmpty(page)||CheckUtil.objIsEmpty(pageSize)||CheckUtil.objIsEmpty(dataScope)||CheckUtil.objIsEmpty(sysUserId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        PageVo pageVo = new PageVo();
        List<CmsEvaluationListRes> resultList=new ArrayList<>();
        pageVo.setItems(resultList);
        pageVo.setTotal(0L);
        //分页查询
        IPage<OaOrderEvaluation> oaOrderEvaluationIPage=getEvalutionList(queryType,userName,mobileAccount,orderNo,sysUserId,dataScope,page,pageSize);
        if(CheckUtil.objIsEmpty(oaOrderEvaluationIPage)){
            return Rv.wrap(pageVo);
        }
        List<Long> driverIdList=new ArrayList<>();
        Map<Long,List<Long>> contentMap=new HashMap<>();
        for (OaOrderEvaluation r:oaOrderEvaluationIPage.getRecords()) {
            driverIdList.add(r.getUserId());
            List<Long> list=new ArrayList<>();
            if(r.getTagCommentContent().contains(",")){
                String[] str=r.getTagCommentContent().split(",");
                for(int i=0;i<str.length;i++){
                    list.add(Long.parseLong(str[i].trim()));
                }
            }else{
                list.add(Long.parseLong(r.getTagCommentContent().trim()));
            }
            contentMap.put(r.getEvaluationId(),list);
        }
        //查询所有的评价信息
        List<OaSysComment> commentList=getCommentList();
        //查询司机信息
        List<OaDUser> driverList=getDriverList(driverIdList);
        //查询用户信息
        List<OaCUser> userList=getCuserList(driverIdList);
        //设置返回结果
        CmsEvaluationListRes res=null;
        for (OaOrderEvaluation r:oaOrderEvaluationIPage.getRecords()) {
            res=new CmsEvaluationListRes();
            res.setEvaluationId(r.getEvaluationId());
            res.setEvaluationContent(r.getEvaluationContent());
            res.setCreateTime(new DateToStringConverter().convert(r.getCreateTime()));
            if(EDriverComingUserType.user.getType().equals(queryType)){
                if(CheckUtil.objIsNotEmpty(userList)){
                    for(OaCUser c:userList){
                        if(c.getUserId().equals(r.getUserId())){
                            res.setMobileAccount(c.getUserPhone());
                            res.setUserName(c.getUserName());
                        }
                    }
                }
            }else{
                //司机
                if(CheckUtil.objIsNotEmpty(driverList)){
                    for(OaDUser d:driverList){
                        if(d.getDriverId().equals(r.getUserId())){
                            res.setMobileAccount(d.getDriverPhone());
                            res.setUserName(d.getDriverName());
                        }
                    }
                }
            }
            if(CheckUtil.objIsNotEmpty(commentList)){
                List<Long> contIdList=contentMap.get(r.getEvaluationId());
                StringBuffer sbf=new StringBuffer();
                for(Long id:contIdList){
                    sbf.append(" ");
                    for(OaSysComment c:commentList){
                        if(id.equals(c.getCommentId())){
                            sbf.append(c.getCommentContent()+",");
                        }
                    }
                }
                res.setTagCommentContent(sbf.toString());
            }
            res.setEvaluationScore(r.getEvaluationScore());
            // 订单号
            String rOrderNo = r.getOrderNo();
            // 订单类型
            if (CheckUtil.objIsNotEmpty(rOrderNo)){
                if (rOrderNo.contains(EOrderType.ZFJC.getDesc())){
                    res.setOrderNoTypeStr(EOrderType.ZFJC.getMsg());
                } else if (rOrderNo.contains(EOrderType.GRGW.getDesc())){
                    res.setOrderNoTypeStr(EOrderType.GRGW.getMsg());
                } else if (rOrderNo.contains(EOrderType.BMGW.getDesc())){
                    res.setOrderNoTypeStr(EOrderType.BMGW.getMsg());
                } else if (rOrderNo.contains(EOrderType.SPGW.getDesc())){
                    res.setOrderNoTypeStr(EOrderType.SPGW.getMsg());
                } else if (rOrderNo.contains(EOrderType.BKCP.getDesc())){
                    res.setOrderNoTypeStr(EOrderType.BKCP.getMsg());
                } else {
                    res.setOrderNoTypeStr("");
                }
                res.setOrderNo(rOrderNo);
            } else {
                res.setOrderNo("");
            }


            resultList.add(res);
        }
        pageVo.setItems(resultList);
        pageVo.setTotal(oaOrderEvaluationIPage.getTotal());
        //返回查询结果
        return Rv.wrap(pageVo);
    }

    /**
     * 通过姓名和手机号码查询司机信息
     * @param userName
     * @param mobielAccount
     * @return
     */
    private IPage<OaOrderEvaluation> getEvalutionList(Integer queryType, String userName, String mobielAccount,String orderNo, Long sysUserId, List<String> dataScope, Integer page, Integer pageSize){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.orderByDesc(OaOrderEvaluation.CREATE_TIME);
        queryWrapper.eq(OaOrderEvaluation.TAG,TokenUtil.getTag());
        queryWrapper.eq(OaOrderEvaluation.USER_TYPE,queryType);
        if(EDriverComingUserType.user.getType().equals(queryType)){
            //查询用户
            List<Long> idList=iOaSysGetOfficeIdListService.selectCUserIdList(sysUserId,dataScope);
            queryWrapper.in(OaOrderEvaluation.USER_ID,idList);
            //通过姓名查询信息
            if(CheckUtil.objIsNotEmpty(userName)||CheckUtil.objIsNotEmpty(mobielAccount)){
                //通过姓名查询用户信息d
                List<OaCUser> userList=getCuserList(idList,userName,mobielAccount);
                if(CheckUtil.objIsEmpty(userList)){
                    return  null;
                }
                List<Long> userIdList=new ArrayList<>();
                for(OaCUser v:userList){
                    userIdList.add(v.getUserId());
                }
                queryWrapper.in(OaOrderEvaluation.USER_ID,userIdList);
            }
        }else{
            //查询司机
            List<Long> idList=iOaSysGetOfficeIdListService.selectDUserIdList(sysUserId,dataScope);
            queryWrapper.in(OaOrderEvaluation.USER_ID,idList);
            //通过姓名查询信息
            if(CheckUtil.objIsNotEmpty(userName)||CheckUtil.objIsNotEmpty(mobielAccount)){
                //通过姓名查询用户信息d
                List<OaDUser> userList=getDriverList(idList,userName,mobielAccount);
                if(CheckUtil.objIsEmpty(userList)){
                    return  null;
                }
                List<Long> driverIdList=new ArrayList<>();
                for(OaDUser v:userList){
                    driverIdList.add(v.getDriverId());
                }
                queryWrapper.in(OaOrderEvaluation.USER_ID,driverIdList);
            }
        }
        if(CheckUtil.objIsNotEmpty(orderNo)){
            queryWrapper.like(OaOrderEvaluation.ORDER_NO,orderNo);
        }
        IPage<OaOrderEvaluation> iPage = new Page<>();
        iPage.setCurrent(page);
        iPage.setSize(pageSize);
        IPage<OaOrderEvaluation> oaDUsers=oaOrderEvaluationMapper.selectPage(iPage, queryWrapper);
        return  oaDUsers;
    }


    /**
     * 通过姓名和手机号码查询司机信息
     * @param userName
     * @param mobielAccount
     * @return
     */
    private List<OaDUser> getDriverList( List<Long> idList,String userName,String mobielAccount){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaDUser.TAG,TokenUtil.getTag());
        queryWrapper.in(OaDUser.DRIVER_ID,idList);
        if(CheckUtil.objIsNotEmpty(userName)){
            //通过姓名查询司机信息
            queryWrapper.like(OaDUser.DRIVER_NAME,userName);
        }
        if(CheckUtil.objIsNotEmpty(mobielAccount)){
            //通过手机号码查询司机信息
            queryWrapper.like(OaDUser.DRIVER_PHONE,mobielAccount);
        }
        List<OaDUser> oaDUsers=oadUserMapper.selectList(queryWrapper);
        return  oaDUsers;
    }

    /**
     * 通过姓名和手机号码查询用户信息
     * @param userName
     * @param mobielAccount
     * @return
     */
    private List<OaCUser> getCuserList(List<Long> idList, String userName, String mobielAccount){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaCUser.TAG,TokenUtil.getTag());
        queryWrapper.in(OaCUser.USER_ID,idList);
        if(CheckUtil.objIsNotEmpty(userName)){
            //通过姓名查询司机信息
            queryWrapper.like(OaCUser.USER_NAME,userName);
        }
        if(CheckUtil.objIsNotEmpty(mobielAccount)){
            //通过手机号码查询司机信息
            queryWrapper.like(OaCUser.USER_PHONE,mobielAccount);
        }
        List<OaCUser> oaDUsers=oacUserMapper.selectList(queryWrapper);
        return  oaDUsers;
    }

    /**
     * 通过id查询司机信息
     * @param idList
     * @return
     */
    private List<OaDUser> getDriverList( List<Long> idList){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaDUser.TAG,TokenUtil.getTag());
        queryWrapper.in(OaDUser.DRIVER_ID,idList);
        List<OaDUser> oaDUsers=oadUserMapper.selectList(queryWrapper);
        return  oaDUsers;
    }

    /**
     * 通过id查询用户信息
     * @param idList
     * @return
     */
    private List<OaCUser> getCuserList( List<Long> idList){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaCUser.TAG,TokenUtil.getTag());
        queryWrapper.in(OaCUser.USER_ID,idList);
        List<OaCUser> oacUsers=oacUserMapper.selectList(queryWrapper);
        return  oacUsers;
    }
    /**
     *查询评价信息
     * @return
     */
    private List<OaSysComment> getCommentList(){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaSysComment.TAG,TokenUtil.getTag());
        List<OaSysComment> oacUsers=oaSysCommentMapper.selectList(queryWrapper);
        return  oacUsers;
    }
}
